package com.example.myapplication.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.activity.LoadingActivity;
import com.example.myapplication.R;
import com.example.myapplication.adapter.recyclerViewAdapter.CommentVideoAdapter;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.CommentVideo;
import com.example.myapplication.entity.RecommendVideo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CommentVideoFragment extends Fragment {
    private View layoutView;
    private EditText edit_comment;
    private TextView send_comment;
    private RecyclerView comment_recycler;
    private CommentVideoAdapter adapter;
    private RecommendVideo.DatasBean datasBean;
    private RecyclerView.LayoutManager manager;
    private List<CommentVideo.HotCommentsBean> hotCommentsBeanList = new ArrayList<>();

    public CommentVideoFragment() {
    }

    public static CommentVideoFragment newInstance(Bundle bundle) {
        CommentVideoFragment fragment = new CommentVideoFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_comment, container, false);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        layoutView = view.findViewById(R.id.layoutView);
        //点击获取焦点
        layoutView.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                layoutView.setFocusable(true);
                layoutView.setFocusableInTouchMode(true);
                layoutView.requestFocus();
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(edit_comment.getWindowToken(), 0);
                return false;
            }
        });

        edit_comment = view.findViewById(R.id.edit_comment);
        send_comment = view.findViewById(R.id.send_comment);
        send_comment.setEnabled(false);

        edit_comment.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(!TextUtils.isEmpty(edit_comment.getText().toString())){
                    send_comment.setEnabled(true);
                }
                else{
                    send_comment.setEnabled(false);
                }
            }
        });
        edit_comment.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                    SpannableString ss = new SpannableString("这里还缺一条你的优质评论");//定义hint的值
                    AbsoluteSizeSpan ass = new AbsoluteSizeSpan(12, true);//设置字体大小 true表示单位是sp
                    ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    edit_comment.setHint(new SpannedString(ss));
                    edit_comment.setTextSize(12);
                    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput( 0 , InputMethodManager.SHOW_FORCED);

                } else {
                    // 此处为失去焦点时的处理内容
                    SpannableString ss = new SpannableString("这里还缺一条你的优质评论");//定义hint的值
                    AbsoluteSizeSpan ass = new AbsoluteSizeSpan(14, true);//设置字体大小 true表示单位是sp
                    ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    edit_comment.setHint(new SpannedString(ss));
                    edit_comment.setTextSize(14);
                }
            }
        });
        send_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommentVideo.HotCommentsBean hotCommentsBean = new CommentVideo.HotCommentsBean();
                hotCommentsBean.setLikedCount(0);
                hotCommentsBean.setContent(edit_comment.getText().toString());
                hotCommentsBean.setTime(new Date().getTime());
                CommentVideo.HotCommentsBean.UserBean userBean = new CommentVideo.HotCommentsBean.UserBean();
                userBean.setNickname(LoadingActivity.profileBean.getNickname());
                userBean.setAvatarUrl(LoadingActivity.profileBean.getAvatarUrl());
                hotCommentsBean.setUser(userBean);
                hotCommentsBeanList.add(hotCommentsBean);
                adapter.setData(hotCommentsBeanList);
                edit_comment.setText("");
                manager.scrollToPosition(adapter.getItemCount()-1);
            }
        });


        comment_recycler = view.findViewById(R.id.comment_recycler);
        manager = new LinearLayoutManager(getContext());
        comment_recycler.setLayoutManager(manager);
        adapter = new CommentVideoAdapter(getContext());
        comment_recycler.setAdapter(adapter);
        datasBean = (RecommendVideo.DatasBean) getArguments().getSerializable("video");
        loadData(datasBean.getData().getVid());
    }

    private void loadData(String id ) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);

        Call<CommentVideo> call = service.getCommentVideo(id);
        call.enqueue(new Callback<CommentVideo>() {
            @Override
            public void onResponse(Call<CommentVideo> call, Response<CommentVideo> response) {
                CommentVideo baseResponse = response.body();
                hotCommentsBeanList = baseResponse.getHotComments();
                adapter.setData(hotCommentsBeanList);
            }

            @Override
            public void onFailure(Call<CommentVideo> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}
